from django.db import models
from datetime import datetime, date
from django.contrib.auth.models import User, Group

make_choice = lambda ls: [(ls.index(s), s) for s in ls]

class DlogIssue(models.Model):
    
    create_by = models.ForeignKey(User)
    create_at = models.DateTimeField(default=datetime.now())
    content = models.TextField(maxlength=5120)
    plan = models.TextField(maxlength=5120)
    
    def __str__(self): return self.content
    
    class Admin:
        list_display = ('create_by', 'create_at', 'content', 'plan')

class SysNofifyIssue(models.Model):
    
    TODO, NEWFEATURE, ANNOUNCE = 0, 1, 2
    TYPES = ['todo', 'new', 'notice']

    suggest_by = models.ForeignKey(User)
    create_at = models.DateTimeField(default=datetime.now())
    content = models.TextField(maxlength=5120)
    ntype = models.IntegerField(choices=make_choice(TYPES))
    
    def __str__(self): return self.content
    
    class Admin:
        list_display = ('ntype', 'suggest_by', 'create_at', 'content')
        
class BlackListIssue(models.Model):
    
    def setList(self, users):
        self.users = ';'.join([str(user) for user in list(users)])
        
    def getList(self):
        if self.users == '':
            return []
        return [int(user) for user in self.users.split(';')]
        
    create_at = models.DateField(default=date.today())
    users = models.CharField(maxlength=512,default="")
    report_sended = models.IntegerField(default=0)
    
    def __str__(self): return str(self.create_at)
    
    class Admin:
        list_display = ('create_at', 'users')
        
class CommentIssue(models.Model):
    
    create_by = models.ForeignKey(User)
    target = models.ForeignKey(DlogIssue)
    create_at = models.DateTimeField(default=datetime.now())
    content = models.TextField(maxlength=5120)
    readed = models.BooleanField(default=False)

    class Admin:
        list_display = ('create_by', 'create_at', 'readed', 'content', 'target')
    